package org.openflashchart.chartdemo;

import java.io.IOException;
import java.util.ArrayList;
import java.util.List;

import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.openflashchart.chart.Chart;
import org.openflashchart.component.Labels;
import org.openflashchart.component.Title;
import org.openflashchart.component.ToolTip;
import org.openflashchart.component.X_Axis;
import org.openflashchart.component.Y_Axis;
import org.openflashchart.elements.Area_Hollow;
import org.openflashchart.elements.Bar;
import org.openflashchart.elements.Bar_3d;
import org.openflashchart.elements.Bar_Fade;
import org.openflashchart.elements.Bar_Glass;
import org.openflashchart.elements.Bar_Sketch;
import org.openflashchart.elements.Bar_Stack;
import org.openflashchart.elements.Filled_Bar;
import org.openflashchart.elements.HBar;
import org.openflashchart.elements.Line;
import org.openflashchart.elements.Line_Dot;
import org.openflashchart.elements.Line_Hollow;
import org.openflashchart.elements.Pie;
import org.openflashchart.elements.Scatter;


/**
 * 
 * @author fanhaibo
 *
 * @time Aug 8, 20089:08:03 AM
 */		
@SuppressWarnings("unchecked")
public class ShowChartServlet extends HttpServlet {

	private static final long serialVersionUID = 8953556519136570651L;

	private static final String AREA_HOLLOW = "area_hollow";

	private static final String LINE = "line";

	private static final String LINE_DOT = "line_dot";

	private static final String LINE_HOLLOW = "line_hollow";

	private static final String SCATTER = "scatter";

	private static final String PIE = "pie";

	private static final String BAR = "bar";

	private static final String BAR_FADE = "bar_fade";

	private static final String BAR_3D = "bar_3d";

	private static final String FILLED_BAR = "filled_bar";

	private static final String BAR_GLASS = "bar_glass";

	private static final String HBAR = "hbar";

	private static final String BAR_SKETCH = "bar_sketch";

	private static final String BAR_STACK = "bar_stack";

	/**
	 * The doPost method of the servlet. <br>
	 * 
	 * This method is called when a form has its tag value method equals to
	 * post.
	 * 
	 * @param request
	 *            the request send by the client to the server
	 * @param response
	 *            the response send by the server to the client
	 * @throws ServletException
	 *             if an error occurred
	 * @throws IOException
	 *             if an error occurred
	 */
	public void doPost(HttpServletRequest request, HttpServletResponse response)
			throws ServletException, IOException {
		String graphType = request.getParameter("graphType");
		if (null != graphType) {
			if (LINE.equalsIgnoreCase(graphType)) {
				showLine(request, response);
			} else if (LINE_DOT.equalsIgnoreCase(graphType)) {
				showLine_Dot(request, response);
			} else if (LINE_HOLLOW.equalsIgnoreCase(graphType)) {
				showLine_Hollow(request, response);
			} else if (SCATTER.equalsIgnoreCase(graphType)) {
				showScatter(request, response);
			} else if (AREA_HOLLOW.equalsIgnoreCase(graphType)) {
				showArea_Hollow(request, response);
			} else if (PIE.equalsIgnoreCase(graphType)) {
				showPie(request, response);
			} else if (BAR.equalsIgnoreCase(graphType)) {
				showBar(request, response);
			} else if (BAR_FADE.equalsIgnoreCase(graphType)) {
				showBar_Fade(request, response);
			} else if (BAR_3D.equalsIgnoreCase(graphType)) {
				showBar_3d(request, response);
			} else if (FILLED_BAR.equalsIgnoreCase(graphType)) {
				showFilled_Bar(request, response);
			} else if (BAR_GLASS.equalsIgnoreCase(graphType)) {
				showBar_Glass(request, response);
			} else if (HBAR.equalsIgnoreCase(graphType)) {
				showHBar(request, response);
			} else if (BAR_SKETCH.equalsIgnoreCase(graphType)) {
				showBar_Sketch(request, response);
			} else if (BAR_STACK.equalsIgnoreCase(graphType)) {
				showBar_Stack(request, response);
			} else {
				response.getWriter().write("{}");
			}
		} else {
			response.getWriter().write("{}");
		}

	}

	/**
	 * Constructor of the object.
	 */
	public ShowChartServlet() {
		super();
	}

	/**
	 * Initialization of the servlet. <br>
	 * 
	 * @throws ServletException
	 *             if an error occure
	 */
	public void init() throws ServletException {
		// Put your code here
	}

	/**
	 * Destruction of the servlet. <br>
	 */
	public void destroy() {
		super.destroy(); // Just puts "destroy" string in log
		// Put your code here
	}

	/**
	 * The doGet method of the servlet. <br>
	 * 
	 * This method is called when a form has its tag value method equals to get.
	 * 
	 * @param request
	 *            the request send by the client to the server
	 * @param response
	 *            the response send by the server to the client
	 * @throws ServletException
	 *             if an error occurred
	 * @throws IOException
	 *             if an error occurred
	 */
	public void doGet(HttpServletRequest request, HttpServletResponse response)
			throws ServletException, IOException {

		doPost(request, response);
	}

	private void showScatter(HttpServletRequest request,
			HttpServletResponse response) throws IOException {
		Chart chart = new Chart();

		X_Axis x_axis = new X_Axis();
		x_axis.setColour("#FF0000");
		x_axis.setMin(-2);
		x_axis.setMax(2);

		Y_Axis y_axis = new Y_Axis();
		y_axis.setColour("#00FF00");
		y_axis.setMin(-2);
		y_axis.setMax(2);

		Scatter scatter = new Scatter("My name is Scatter");
		scatter.setDot__size(3);
		List values = new ArrayList();
		for (int i = 0; i < 360; i += 5) {
			values.add("{x:" + Math.sin(i) + ",y:" + Math.cos(i) + "}");
		}

		scatter.setValues(values);
		chart.setScatter(scatter);
		chart.setX_Axis(x_axis);
		chart.setY_Axis(y_axis);
		response.getWriter().write(chart.createChart());
	}

	@SuppressWarnings("unchecked")
	private void showPie(HttpServletRequest request,
			HttpServletResponse response) throws IOException {
		Chart chart = new Chart();
		Title title = new Title();
		title.setText("My name is Title");
		title.setStyle("{colour:#DDDDDD;font-size: 15px;}");

		Pie pie = new Pie();
		pie.setAlpha(0.6);
		pie.setAnimate(true);
		pie.setBorder(6.0);
		pie.setTip("hello");
		pie.setValues(new Object[] { 21, 29, 15, 10, 13, 12 });
		pie.setColours(new Object[] { "#225599", "#995522", "#3399cc",
				"#cc9933", "#551188" });

		chart.setPie(pie);
		chart.setTitle(title);
		chart.setBg_colour("#66DD33");
		response.getWriter().write(chart.createChart());
	}

	/**
	 * { "elements": [ { "width": 4, "dot-size": 10, "type": "line", "values":
	 * [2,4,5,7,11,16,17,17.3,18], "text": "My name is Line", "colour":
	 * "#CCDDEE", "font-size": 12 }] ,
	 * 
	 * "x_axis":{ "colour": "#FF0000", "labels":{ "labels":
	 * ["a","b","c","d","e","f","g","h","i"] } },
	 * 
	 * "y_axis":{ "steps": 2, "colour": "#00FF00", "max": 20 } }
	 * 
	 * Function:
	 * 
	 * @param request
	 * @param response
	 * @throws ServletException
	 * @throws IOException
	 *             void
	 * 
	 */
	@SuppressWarnings("unchecked")
	private void showLine(HttpServletRequest request,
			HttpServletResponse response) throws ServletException, IOException {
		Chart chart = new Chart();
		Title title = new Title();
		title.setText("My name is Title");
		title.setStyle("{color: #736AFF;font-size: 30px;}");

		X_Axis x_axis = new X_Axis();
		x_axis.setColour("#FF0000");
		List x_axisLabels = new ArrayList();
		Labels labels = new Labels();
		x_axisLabels.add("a");
		x_axisLabels.add("b");
		x_axisLabels.add("c");
		x_axisLabels.add("d");
		x_axisLabels.add("e");
		x_axisLabels.add("f");
		x_axisLabels.add("g");
		x_axisLabels.add("h");
		x_axisLabels.add("i");

		labels.setLabels(x_axisLabels);

		x_axis.setLabels(labels);

		Y_Axis y_axis = new Y_Axis();
		y_axis.setColour("#00FF00");
		y_axis.setMax(20);
		y_axis.setSteps(2);

		Line line = new Line("My name is Line");
		line.setColour("#CCDDEE");
		line.setDot__size(10);
		line.setFont__size(12);
		line.setWidth(4);
		line.setValues(new Object[] { 4, 5, 7, 11, 16, 17, 17.3, 18, 18.5 });

		chart.setTitle(title);
		chart.setLine(line);
		chart.setX_Axis(x_axis);
		chart.setY_Axis(y_axis);
		response.getWriter().write(chart.createChart());

	}

	private void showLine_Dot(HttpServletRequest request,
			HttpServletResponse response) throws ServletException, IOException {
		Chart chart = new Chart();

		X_Axis x_axis = new X_Axis();
		x_axis.setColour("#FF0000");
		x_axis.setMax(30);
		String[] x_axisLabels = new String[] { "0.00", "0.38", "0.74", "1.07",
				"1.36", "1.60", "1.77", "1.87", "1.90", "1.85", "1.73", "1.54",
				"1.28", "0.98", "0.64", "0.27", "-0.11", "-0.49", "-0.84",
				"-1.16", "-1.44", "-1.66", "-1.81", "-1.89", "-1.89", "-1.82",
				"-1.68", "-1.47", "-1.20", "-0.88" };

		x_axis.setList_labels(x_axisLabels);

		Y_Axis y_axis = new Y_Axis();
		y_axis.setColour("#00FF00");
		y_axis.setMax(3);

		Line_Dot line_dot = new Line_Dot("My name is Line_Dot");
		line_dot.setHalo__size(2);
		line_dot.setDot__size(3);
		List values = new ArrayList();
		for (double i = 0; i < 8; i += 0.2) {
			double val = Math.sin(i) + 1.5;
			values.add(val);
		}
		line_dot.setValues(values);

		chart.setLine_Dot(line_dot);
		chart.setX_Axis(x_axis);
		chart.setY_Axis(y_axis);
		response.getWriter().write(chart.createChart());
	}

	private void showLine_Hollow(HttpServletRequest request,
			HttpServletResponse response) throws IOException {
		Chart chart = new Chart();

		X_Axis x_axis = new X_Axis();
		x_axis.setColour("#FF0000");
		x_axis.setMax(30);
		String[] x_axisLabels = new String[] { "0.00", "0.38", "0.74", "1.07",
				"1.36", "1.60", "1.77", "1.87", "1.90", "1.85", "1.73", "1.54",
				"1.28", "0.98", "0.64", "0.27", "-0.11", "-0.49", "-0.84",
				"-1.16", "-1.44", "-1.66", "-1.81", "-1.89", "-1.89", "-1.82",
				"-1.68", "-1.47", "-1.20", "-0.88" };

		x_axis.setList_labels(x_axisLabels);

		Y_Axis y_axis = new Y_Axis();
		y_axis.setColour("#00FF00");
		y_axis.setMax(3);

		Line_Hollow line_hollow = new Line_Hollow("My name is Line_Hollow");
		line_hollow.setDot__size(4);
		line_hollow.setHalo__size(2);
		List values = new ArrayList();
		for (double i = 0; i < 8; i += 0.2) {
			double val = Math.sin(i) + 1.5;
			values.add(val);
		}
		line_hollow.setValues(values);

		chart.setLine_Hollow(line_hollow);
		chart.setX_Axis(x_axis);
		chart.setY_Axis(y_axis);
		response.getWriter().write(chart.createChart());

	}

	private void showArea_Hollow(HttpServletRequest request,
			HttpServletResponse response) throws IOException {
		Chart chart = new Chart();

		X_Axis x_axis = new X_Axis();
		x_axis.setColour("#FF0000");
		x_axis.setMax(30);
		String[] x_axisLabels = new String[] { "0.00", "0.38", "0.74", "1.07",
				"1.36", "1.60", "1.77", "1.87", "1.90", "1.85", "1.73", "1.54",
				"1.28", "0.98", "0.64", "0.27", "-0.11", "-0.49", "-0.84",
				"-1.16", "-1.44", "-1.66", "-1.81", "-1.89", "-1.89", "-1.82",
				"-1.68", "-1.47", "-1.20", "-0.88" };

		x_axis.setList_labels(x_axisLabels);

		Y_Axis y_axis = new Y_Axis();
		y_axis.setColour("#00FF00");
		y_axis.setMin(-2);
		y_axis.setMax(2);
		// y_axis.setSteps(2);

		Area_Hollow area_Hollow = new Area_Hollow("My name is Area_Hollow");
		area_Hollow.setColour("#FF");
		area_Hollow.setDot__size(3);
		area_Hollow.setFill("#DD3366");
		area_Hollow.setFill__alpha(0.6);
		area_Hollow.setWidth(2);

		Double[] arrayValues = new Double[] { 0.0, 0.37, 0.73, 1.07, 1.36,
				1.59, 1.77, 1.87, 1.89, 1.85, 1.72, 1.53, 1.28, 0.97, 0.63,
				0.26, -0.11, -0.48, -0.84, -1.16, -1.43, -1.65, -1.80, -1.88,
				-1.89, -1.82, -1.67, -1.46, -1.19, -0.88 };
		area_Hollow.setValues(arrayValues);

		chart.setArea_Hollow(area_Hollow);
		chart.setX_Axis(x_axis);
		chart.setY_Axis(y_axis);
		response.getWriter().write(chart.createChart());

	}

	private void showBar_3d(HttpServletRequest request,
			HttpServletResponse response) throws IOException {
		Chart chart = new Chart();
		X_Axis x_axis = new X_Axis();
		x_axis.setColour("#00EE00");
		x_axis.setSteps(1);
		x_axis.set___3d(6);
		List<Integer> labels = new ArrayList<Integer>();
		labels.add(2);
		labels.add(3);
		labels.add(4);
		labels.add(5);
		x_axis.setList_labels(labels);

		Y_Axis y_axis = new Y_Axis();
		y_axis.setMax(17);

		// ==========3DBar=====================
		Bar_3d bar_3d = new Bar_3d();
		bar_3d.setBar_3d(true);
		bar_3d.setAlpha(0.5);
		bar_3d.setColour("#9933CC");
		bar_3d.setFont__size(10);
		bar_3d.setText("My name is Bar_3d");
		bar_3d.setValues(new Object[] { 7, 9, 6, 11, 8, 4 });

		chart.setBar_3d(bar_3d);
		chart.setX_Axis(x_axis);
		chart.setY_Axis(y_axis);
		chart.setBg_colour("#EEEEEE");
		response.getWriter().write(chart.createChart());
	}

	private void showFilled_Bar(HttpServletRequest request,
			HttpServletResponse response) throws IOException {
		Chart chart = new Chart();
		chart.setBg_colour("#FFFFFF");
		/* Filled_Bar */
		Filled_Bar filled_bar = new Filled_Bar("My name is Filled_Bar");
		filled_bar.setAlpha(0.8);
		filled_bar.setOutline__colour("#577261");
		filled_bar.setColour("#E2D66A");
		filled_bar.setFont__size(10);
		filled_bar.setValues(new Object[] { 9, 8, 7, 6, 5, 4, 3, 2, 1 });

		chart.setFilled_Bar(filled_bar);
		response.getWriter().write(chart.createChart());
	}

	private void showBar_Glass(HttpServletRequest request,
			HttpServletResponse response) throws IOException {
		Chart chart = new Chart();
		X_Axis x_axis = new X_Axis();
		x_axis.setColour("#00EE00");
		Y_Axis y_axis = new Y_Axis();
		y_axis.setMax(15);

		Bar_Glass bar_glass = new Bar_Glass();
		bar_glass.setColour("#3333CC");
		bar_glass.setFont__size(10);
		bar_glass.setText("My name is Bar_Glass");

		List<Object> bar_glassValues = new ArrayList<Object>();
		bar_glassValues.add(7);
		bar_glassValues.add(9);
		bar_glassValues.add(6);
		bar_glassValues.add(10);
		bar_glassValues.add(2);
		bar_glassValues.add(4);
		bar_glass.setValues(bar_glassValues);
		chart.setBar_Glass(bar_glass);
		chart.setX_Axis(x_axis);
		chart.setY_Axis(y_axis);
		chart.setBg_colour("#EEEEEE");
		response.getWriter().write(chart.createChart());
	}

	private void showHBar(HttpServletRequest request,
			HttpServletResponse response) throws IOException {
		Chart chart = new Chart();
		X_Axis x_axis = new X_Axis();
		x_axis.setColour("#00EE00");
		x_axis.setSteps(1);
		x_axis.setList_labels(new Object[] { 2, 3, 4, 5 });
		Y_Axis y_axis = new Y_Axis();
		y_axis.setMin(-1);
		y_axis.setMax(5);

		HBar hbar = new HBar();
		hbar.setText("My name is hbar");
		hbar.setColour("#9933CC");
		hbar.setFont__size(10);
		hbar.setValues(new Object[] { "{right:5}", "{right:9}",
				"{left:13,right:16}" });
		chart.setHBar(hbar);
		chart.setX_Axis(x_axis);
		chart.setY_Axis(y_axis);

		chart.setBg_colour("#EEEEEE");
		response.getWriter().write(chart.createChart());
	}

	private void showBar_Sketch(HttpServletRequest request,
			HttpServletResponse response) throws IOException {
		Chart chart = new Chart();
		X_Axis x_axis = new X_Axis();
		x_axis.setColour("#00EE00");
		x_axis.setSteps(1);
		x_axis.setList_labels(new Object[] { 2, 3, 4, 5 });

		Y_Axis y_axis = new Y_Axis();
		y_axis.setMin(-1);
		y_axis.setMax(15);

		Bar_Sketch bar_sketch = new Bar_Sketch();
		bar_sketch.setColour("#3333CC");
		bar_sketch.setFont__size(10);
		bar_sketch.setText("My name is Bar_Sketch");
		bar_sketch.setValues(new Object[] { 7, 9, 6, 10, 0, 4 });

		chart.setBar_Sketch(bar_sketch);
		chart.setX_Axis(x_axis);
		chart.setY_Axis(y_axis);

		chart.setBg_colour("#EEEEEE");
		response.getWriter().write(chart.createChart());
	}

	private void showBar_Stack(HttpServletRequest request,
			HttpServletResponse response) throws IOException {
		Chart chart = new Chart();
		X_Axis x_axis = new X_Axis();
		x_axis.setColour("#00EE00");
		x_axis.setList_labels(new Object[] { 2, 3, 4, 5 });

		Y_Axis y_axis = new Y_Axis();
		y_axis.setMin(0);
		y_axis.setMax(15);

		Bar_Stack bar_stack = new Bar_Stack();
		bar_stack.setColour("#3333CC");
		bar_stack.setFont__size(10);
		bar_stack.setText("My name is Bar_Stack");
		bar_stack.setValues(new Object[] { new Object[] { 2.5, 5 },
				new Object[] { 7.5 },
				new Object[] { 5, "{val:5,colour:#ff0000}" },
				new Object[] { 2, 2, 2, 2, "{val:2,colour:#ff00ff}" } });

		chart.setBar_Stack(bar_stack);
		chart.setX_Axis(x_axis);
		chart.setY_Axis(y_axis);

		chart.setBg_colour("#EEEEEE");
		response.getWriter().write(chart.createChart());
	}

	private void showBar(HttpServletRequest request,
			HttpServletResponse response) throws IOException {
		Chart chart = new Chart();
		X_Axis x_axis = new X_Axis();
		x_axis.setColour("#00EE00");

		Y_Axis y_axis = new Y_Axis();
		y_axis.setMax(15);

		// ==========Bar=====================
		Bar bar = new Bar();
		bar.setBar_3d(true);
		bar.setAlpha(0.9);
		bar.setColour("#9933CC");
		bar.setFont__size(10);
		bar.setText("My name is bar");
		bar.setValues(new Object[] { 7, 9, 6, 11, null, 4 });
		bar.setTip("Bar 1<br> val = #val#");

		Bar bar2 = new Bar();
		bar2.setBar_3d(true);
		bar2.setAlpha(0.9);
		bar2.setColour("#00AADD");
		bar2.setFont__size(10);
		bar2.setText("My name is bar");
		bar2.setValues(new Object[] { 7, 9, 6, 11, null, 4 });
		bar2.setTip("Bar 2 val = #val#");

		ToolTip toolTip = new ToolTip();
		toolTip.setShadow(false);
		toolTip.setStroke(5);
		toolTip
				.setBody("{font-size: 10px; font-weight: bold; color: #000000;}");
		toolTip.setBackground("#BDB396");
		toolTip.setTitle("{font-size: 14px; color: #CC2A43;}");
		chart.setBar(bar);
		chart.setBar(bar2);
		chart.setToolTip(toolTip);
		chart.setX_Axis(x_axis);
		chart.setY_Axis(y_axis);
		chart.setBg_colour("#EEEEEE");
		response.getWriter().write(chart.createChart());
	}

	private void showBar_Fade(HttpServletRequest request,
			HttpServletResponse response) throws IOException {
		Chart chart = new Chart();
		X_Axis x_axis = new X_Axis();
		x_axis.setColour("#00EE00");
		x_axis.setSteps(1);
		x_axis.setList_labels(new Object[] { 2, 3, 4, 5 });
		Y_Axis y_axis = new Y_Axis();
		y_axis.setMax(15);
		// ==========Bar=====================
		Bar_Fade bar_fade = new Bar_Fade();
		bar_fade.setBar_3d(true);
		bar_fade.setAlpha(0.9);
		bar_fade.setColour("#9933CC");
		bar_fade.setFont__size(10);
		bar_fade.setText("My name is Bar_Fade");
		bar_fade.setValues(new Object[] { 7, 9, 6, 11, 8, 4 });

		chart.setBar_Fade(bar_fade);
		chart.setX_Axis(x_axis);
		chart.setY_Axis(y_axis);
		chart.setBg_colour("#EEEEEE");
		response.getWriter().write(chart.createChart());
	}

}
